package targaman.moltenwow.autovoter.services;

import targaman.moltenwow.autovoter.controllers.ConfigurationImpl;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by Jonathan on 30/12/2014.
 */
public abstract class Configuration {

    String key = ""; // TODO external key??
    String fileName = "molten.dat";
    Map<String, String> configFile = new HashMap<>();

    public abstract void setup();

    public void createOrReplaceConfigurationFile() {
        String configurationFile = "";
        Set<String> keys = configFile.keySet();
        for (String key : keys) {
            configurationFile += key + ":" + configFile.get(key) + "\n";
        }
        BufferedWriter writer = null;
        try {
            writer = new BufferedWriter(new FileWriter(fileName));
            writer.write(configurationFile);

        } catch (IOException e) {
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (IOException e) {
            }
        }
    }

    public void addToConfigurationList(String key, String value) {
        if (configFile.containsKey(key)) {
            configFile.replace(key, value);
        } else {
            configFile.put(key, value);
        }
    }

    public boolean hasConfigurationInList(String key) {
        if (configFile.containsKey(key)) {
            return true;
        }
        return false;
    }

    public String getValueFromKeyInConfiguration(String key) {
        if (configFile.containsKey(key))
            return configFile.get(key);
        return null;

    }


    public boolean removeFromConfigurationList(String key, String value) {
        if (configFile.containsKey(key)) {
            configFile.remove(key);
            return true;
        }
        return false;
    }

    public boolean getConfigurationFile() {
        File file = new File(fileName);

        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            return false;
        }

        String line = null;
        try {
            while ((line = br.readLine()) != null) {
                if (line.contains(ConfigurationImpl.SET_AUTO_VOTE)//TODO change to enum or some other dynamic implementation
                        || line.contains(ConfigurationImpl.SET_ENABLE_CONSOLE)
                        || line.contains(ConfigurationImpl.MINIMISE_TO_TRAY_ON_STARTUP)
                        || line.contains(ConfigurationImpl.DEFAULT_USERNAME)
                        || line.contains(ConfigurationImpl.SET_AUTO_LOGIN)
                        || line.contains(ConfigurationImpl.DEFAULT_PASSWORD)) {
                    String currentLine[] = line.split(":");
                    String key = currentLine[0];
                    String keyVal = currentLine[1];
                    addToConfigurationList(key, keyVal);
                }
            }
        } catch (IOException e) {
            return false;
        }

        try {
            br.close();
        } catch (IOException e) {
            return false;
        }

        return true;
    }

    public boolean deleteConfigurationFile() {
        return false;
    }

    public String encryptString(String string) {
        return string;
    }

    public List<String> encryptStrings(List<String> strings) {
        return null;
    }


}
